Moduł:Infobox recipe
[=[ -- Implements Template:Infobox recipe --]=] local p = {} local yesno = require('Module:Yesno') local params = require('Module:Paramtest') local commas = require('Module:Addcommas') local geprice = require('Module:Exchange')._price local skillpic = require('Module:Skill_clickpic')._main local editbutton = require('Module:Edit button') local onmain = require('Module:Mainonly').on_main local JSONmod = require('Module:JSON') function p.main(frame) local args = frame:getParent().args -- Function to handle all the materials listed Creates a table to pass to _main() consisting of * Item name, as a wikilink * Item image, as a wikilink, includes link to page * Quantity * Formatted price for display * Clean price for calculations If the parameter mat# is defined and not blank Every parameter (mat#img, mat#qty, mat#price) will be looked for as well -- local function mat_list() local ret_list = {} for i=1,10,1 do local mat = args'mat'..i if mat and params.has_content(mat) then local name = mat -- 2 separate prices -- 1 for display -- 1 for calculation local price,price_raw local qty = params.default_to(args'mat'..i..'qty','1') local img = params.default_to(args'mat'..i..'img', name..'.png') :gsub('File:','') local price_v = params.default_to(args'mat'..i..'price','/') -- gemw = use exchange price if price_v:lower() 'gemw' then price_raw = geprice(name) price = commas._add(price_raw) -- parseable as number = use that elseif tonumber(commas._strip(price_v),10) then price_raw = tonumber(commas._strip(price_v),10) price = commas._add(price_raw) -- Some untradeables have a distinct market price -- because of their materials -- The template will help here -- Look for template call elseif price_v:find(' ') then price_raw = frame:preprocess(price_v) price = commas._add(price_raw) -- everything else -- use N/A for display and 0 for calculations else price_raw = 0 price = 'N/A' end table.insert(ret_list, { name = ''..name..'', name_raw = name, price = price, price_raw = price_raw, quantity = qty, image = ' ', image_raw = img } ) end end return ret_list end -- Function to get a list of additional requirements, should there be any Will take non-blank parameters and simply paste them as is into a table Which will then be passed into the _main() function -- local function get_other_reqs() local req_list = {} for i=1,5,1 do local req = args'misc'..i if req and req:find('%S') then table.insert(req_list,req) end end return req_list end -- Fetching and formatting parameters starting here local item_name,item_image, skill,level,experience = params.defaults{ {args.name,mw.title.getCurrentTitle().text}, {args.image,nil}, {args.skill,nil}, {args.level,editbutton('?')}, {args.skillXP,nil} } local skill2,experience2, quest = params.defaults{ {args.secSkill,nil}, {args.secXP,nil}, {args.quest,nil} } -- Add to image, if it's defined if item_image then item_image = ' ' -- anti-redlink elseif mw.title.getCurrentTitle().text 'Infobox recipe/preload' or mw.title.getCurrentTitle().text 'PreloadPage/Orphanage' then item_image = '' else item_image = ' ' end -- Allow amount to be removed if it's 1 local item_amount = tonumber(args.amount,10) or 1 -- Tables to pass; call above functions local materials = mat_list() local misc_reqs = get_other_reqs() -- Look for gemw in the parameter, otherwise nil local item_ge_v = string.lower(args.geValue or '') local item_gep if item_ge_v 'gemw' then item_gep = geprice(item_name) elseif item_ge_v 'no' then item_gep = 'no' else item_gep = nil end -- Only accept "yes" or "no" -- Anything else defaults to no -- Should this default to unknown instead? local members = yesno(args.members) and 'Yes' or 'No' -- Pass parameters to _main return p._main{ item_name, item_image, item_amount, materials, misc_reqs, skill, level, experience, skill2, experience2, item_gep, members, quest } end -- -- Main -- function p._main(...) -- Unpack parameters local item_name,item_image,item_amount, materials,misc_reqs, skill,level,experience, skill2,experience2,item_gep, members,quest = unpack(...) local ret = mw.html.create('table') :addClass('wikitable') :css({ width = '50%', 'max-width' = '450px' }) -- Caption header local caption = item_image..' '..item_name if item_amount > 1 then caption = caption..' ×'..item_amount end ret:tag('caption'):wikitext(caption):done() -- Creation of row 1 -- Columns 1 and 5 local row1 = ret:tag('tr') :css('text-align','center') local row1a = row1:tag('th') :css('width','20%') :attr('colspan','2') if skill then if skill:lower() 'no' then row1a:wikitext('-') else row1a:wikitext(skillpic(skill)) end else row1a:wikitext('\'\'?\'\' '..editbutton()) end local row1e = row1:tag('th') :css('width','20%') :attr('colspan','2') if skill2 and not (skill2:lower() 'no') then row1e:wikitext(skillpic(skill2)) else row1e:wikitext('-') end row1:tag('th') :attr('title','Grand Exchange median price') :css('width','20%') :wikitext(' ') :done() -- Creation of row 2 local row2 = ret:tag('tr') :css('text-align','center') local row2a = row2:tag('td') :attr('colspan','2') local row2b = row2:tag('td') :attr('colspan','2') local row2c = row2:tag('td') if skill and skill:lower() 'no' then row2a:wikitext('-') elseif experience then row2a:wikitext(commas._add(experience)..' XP') else row2a:wikitext('\'\'?\'\' '..editbutton()) end if experience2 then row2b:wikitext(commas._add(experience2)..' XP') else row2b:wikitext('-') end if item_gep then if item_gep 'no' then row2c:attr('title','Cannot be sold on the Grand Exchange') :wikitext('-') elseif item_amount > 1 then row2c:attr('title',commas._add(item_gep)..' coins each') :wikitext(commas._add(item_gep * item_amount)) else row2c:wikitext(commas._add(item_gep)) end else row2c:wikitext('\'\'?\'\' '..editbutton()) end -- Add "Requirements" header ret:tag('tr') :tag('th') :attr('colspan','5') :wikitext('Requirements') :done() :done() -- Skill requirement 1 if skill and not (skill:lower() 'no') then ret:tag('tr') :tag('td') :attr('colspan','4') :wikitext(skillpic(skill)..' '.. params.ucflc(skill)..' level') :done() :tag('td') :css('text-align','center') :wikitext(level) :done() :done() end -- Quest requirement if quest then ret:tag('tr') :tag('td') :attr('colspan','5') :wikitext(skillpic('quest')..' '..quest) :done() :done() end -- Miscellaneous requirements for i, v in ipairs(misc_reqs) do ret:tag('tr') :tag('td') :attr('colspan','5') :wikitext(v) :done() :done() end -- Members requirement ret:tag('tr') :tag('td') :attr('colspan','4') :wikitext(' Members only') :done() :tag('td') :css('text-align','center') :wikitext(members) :done() :done() -- Materials header ret:tag('tr') :tag('th') :attr('colspan','5') :wikitext('Materials') :done() :done() :tag('tr') :tag('th') :attr('colspan','2') :wikitext('Item') :done() :tag('th') :wikitext('Quantity') :done() :tag('th') :wikitext('Price') :done() :tag('th') :wikitext('Total') :done() :done() -- Materials local total_price = 0 local valued_items = 0 -- Iterates through the tables in the table of materials for i, v in ipairs(materials) do -- Fetch all the variables local mat_name = v.name local mat_price = v.price local mat_raw = v.price_raw local mat_ttl local mat_qty = v.quantity local mat_img = v.image -- If no price is given for item, delete total -- Otherwise quantity * price each if mat_raw 0 then mat_ttl = '-' else mat_ttl = mat_qty * mat_raw end -- If item total is a number -- Add it to the overall total -- Indicate this by incrementing valued item count -- Done with total calculations, add commas to total for formatting if type(mat_ttl) 'number' then total_price = total_price + mat_ttl mat_ttl = commas._add(mat_ttl) if mat_raw > 0 then valued_items = valued_items + 1 end end ret:tag('tr') :tag('td') :css({ 'border-right' = 'none', 'text-align' = 'center' }) :wikitext(mat_img) :done() :tag('td') :css({ 'border-left' = 'none', 'border-right' = 'none', 'text-align' = 'left' }) :wikitext(mat_name) :done() :tag('td') :css({ 'border-left' = 'none', 'border-right' = 'none', 'text-align' = 'right' }) :wikitext(commas._add(mat_qty)) :done() :tag('td') :css({ 'border-left' = 'none', 'text-align' = 'right' }) :wikitext(mat_price) :done() :tag('td') :css('text-align','right') :wikitext(mat_ttl) :done() :done() end -- If no materials found -- Indicate nothing listed, add an edit button for the page if #materials 0 then ret:tag('tr') :tag('td') :attr('colspan','5') :css({ 'font-style' = 'italic', 'text-align' = 'center' }) :wikitext('Materials unlisted '..editbutton()) :done() :done() end -- Total -- Hide if less than 2 items have a value if valued_items > 1 then ret:tag('tr') :tag('th') :attr('colspan','4') :css('text-align','right') :wikitext('Total price') :done() :tag('td') :css('text-align','right') :wikitext(commas._add(total_price)) :done() :done() end if onmain then smw = {} table.insert(smw,'Required materials:') local jsonmats = {} for _, v in ipairs(materials) do table.insert(jsonmats,string.format('{ "name": "%s", "quantity": "%s", "image": "%s" }', v.name_raw, v.quantity, v.image_raw )) table.insert(smw,string.format('Made from item::%s',v.name_raw)) end json = JSONmod.wikiescapes(json) table.insert(smw,'- - - - - - - - - -') table.insert(smw,'Production JSON:') table.insert(smw,string.format('Production JSON::%s',json)) ret :tag('div') :attr('smw-infobox-data') :addClass('hidden') :css('display','none') :tag('pre') :wikitext(table.concat(smw,'\n')) :done() :done() end return ret end return p